package dca.sistemascontrole.view;

import dca.sistemascontrole.util.ControleConstantes;

/**
 *
 * @author rafael
 */
public class PanelAnalises extends javax.swing.JPanel {

    private long t0, ta, t1;
    private double pv, pvAnt;
    private double setpoint;
    private double tempoPico;
    private double tempoSubida;
    private double tempoAcomodacao2;
    private double tempoAcomodacao5;
    private double tempoAcomodacao7;
    private double maxSobreSinal;
    private double derivadaAnt, derivada;
    private boolean tempo2Determinado;
    private boolean tempo5Determinado;
    private boolean tempo7Determinado;
    private boolean tempoSubidaDeterminado;

    public PanelAnalises() {
        initComponents();
    }

    public void calcularAnalises(double setpoint, double nivelTanqueSuperior, double nivelTanqueInferior) {
        if (ControleConstantes.PV_IS_TANQUE_SUPERIOR) {
            pv = nivelTanqueSuperior;
        } else {
            pv = nivelTanqueInferior;
        }

        setSetpoint(setpoint, pv);

        t1 = System.currentTimeMillis();

        derivada = (pv - pvAnt) / (t1 - ta);

        calcularSobresinal_tPico();
        calcularTempoSubida();
        calcularTempoAcomodacao();

        ta = t1;
        pvAnt = pv;
        derivadaAnt = derivada;
    }

    private void setSetpoint(double setpoint, double pv) {
        if (this.setpoint != setpoint) {
            txtTempoPico.setText("--");
            txtSobreSinal.setText("--");
            txtTempoSubida.setText("--");
            txtTempoAcomodacao2.setText("--");

            tempo2Determinado = false;
            tempo5Determinado = false;
            tempo7Determinado = false;
            tempoSubidaDeterminado = false;

            this.setpoint = setpoint;

            tempoPico = 0;
            tempoSubida = 0;
            derivada = 0;
            derivadaAnt = 0;
            maxSobreSinal = 0;

            t0 = System.currentTimeMillis();
            ta = t0;
            pvAnt = pv;
        }
    }

    private void calcularSobresinal_tPico() {
        if (derivadaAnt > 0 && derivada <= 0 && pv > maxSobreSinal) {
            maxSobreSinal = pv;
            tempoPico = (t1 - t0) / 1000.0;
            double mp = ((maxSobreSinal - setpoint) / setpoint) * 100;

            txtTempoPico.setText(String.format("%.2f s", tempoPico));
            txtSobreSinal.setText(String.format("%.2f %%", mp));
        }
    }

    private void calcularTempoSubida() {
        if (pv > setpoint && !tempoSubidaDeterminado) {
            tempoSubidaDeterminado = true;
            tempoSubida = (t1 - t0) / 1000.0;
            txtTempoSubida.setText(String.format("%.2f s", tempoSubida));
        }
    }

    private void calcularTempoAcomodacao() {
        if ((pv / setpoint) > 0.98 && (pv / setpoint) < 1.02) {
            if (!tempo2Determinado) {
                tempo2Determinado = true;
                tempoAcomodacao2 = (t1 - t0)/1000.0;
                txtTempoAcomodacao2.setText(String.format("%.2f s", tempoAcomodacao2));
            }
        } else {
            tempo2Determinado = false;
            tempoAcomodacao2 = Double.MAX_VALUE;
            txtTempoAcomodacao2.setText("--");
        }

        if ((pv / setpoint) > 0.95 && (pv / setpoint) < 1.05) {
            if (!tempo5Determinado) {
                tempo5Determinado = true;
                tempoAcomodacao5 = (t1 - t0)/1000.0;
                txtTempoAcomodacao5.setText(String.format("%.2f s", tempoAcomodacao5));
            }
        } else {
            tempo5Determinado = false;
            tempoAcomodacao5 = Double.MAX_VALUE;
            txtTempoAcomodacao5.setText("--");
        }

        if ((pv / setpoint) > 0.93 && (pv / setpoint) < 1.07) {
            if (!tempo7Determinado) {
                tempo7Determinado = true;
                tempoAcomodacao7 = (t1 - t0)/1000.0;
                txtTempoAcomodacao7.setText(String.format("%.2f s", tempoAcomodacao7));
            }
        } else {
            tempo7Determinado = false;
            tempoAcomodacao7 = Double.MAX_VALUE;
            txtTempoAcomodacao7.setText("--");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTempoSubida = new javax.swing.JTextField();
        txtSobreSinal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtTempoPico = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTempoAcomodacao7 = new javax.swing.JTextField();
        txtTempoAcomodacao5 = new javax.swing.JTextField();
        txtTempoAcomodacao2 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Análise"));

        jLabel1.setText("Tempo Subida:");

        txtTempoSubida.setEditable(false);

        txtSobreSinal.setEditable(false);

        jLabel2.setText("Sobre-sinal %:");

        jLabel3.setText("Tempo de pico:");

        txtTempoPico.setEditable(false);

        jLabel4.setText("Tempo Acomdação (2%):");

        jLabel5.setText("Tempo Acomdação (5%):");

        jLabel6.setText("Tempo Acomdação (7%):");

        txtTempoAcomodacao7.setEditable(false);

        txtTempoAcomodacao5.setEditable(false);

        txtTempoAcomodacao2.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoSubida, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSobreSinal))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoPico))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoAcomodacao2, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoAcomodacao5, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoAcomodacao7, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTempoSubida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSobreSinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTempoPico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTempoAcomodacao2)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTempoAcomodacao5)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTempoAcomodacao7)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, txtSobreSinal, txtTempoPico, txtTempoSubida});

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Valores"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 238, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 163, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtSobreSinal;
    private javax.swing.JTextField txtTempoAcomodacao2;
    private javax.swing.JTextField txtTempoAcomodacao5;
    private javax.swing.JTextField txtTempoAcomodacao7;
    private javax.swing.JTextField txtTempoPico;
    private javax.swing.JTextField txtTempoSubida;
    // End of variables declaration//GEN-END:variables
}
